package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.Order;
import com.situ.mall.entity.Shipping;
import com.situ.mall.mapper.OrderMapper;
import com.situ.mall.service.IOrderService;

@Service
public class OrderServiceImpl implements IOrderService {
	@Autowired
	private OrderMapper orderMapper;
	
	@Override
	public ServerResponse addOrder(Order order) {
		try {
			Integer count = orderMapper.insert(order);
			if (count == 1) {
				return ServerResponse.createSuccess("order添加成功");
			} else {
				return ServerResponse.createSuccess("order添加失败");
			}
		} catch (Exception e) {
			return ServerResponse.createSuccess("order添加失败");
		}
	}

	@Override
	public List<Order> selectOrder(Integer id) {
		return orderMapper.selectOrder(id);
	}


	//页面展示
	@Override
	public ServerResponse pageList(Integer page, Integer limit, Order order) {
		//分页插件
		PageHelper.startPage(page,limit);
		//查询
		List<Order> list = orderMapper.pageList(order);
		//封装pageInfo
		PageInfo pageInfo = new PageInfo<>(list);
		//拿到总数据
		Integer count = (int)pageInfo.getTotal();
		return ServerResponse.createSuccess("查询成功", count, list);
	}

	//删除订单
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = orderMapper.deleteByPrimaryKey(id);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return ServerResponse.createError("删除失败");
	}

	//批量删除订单
	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArray = ids.split(",");//字符串转换字符数组
		try {
			Integer count = orderMapper.deleteAll(idArray);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return ServerResponse.createError("删除失败");
	}

}
